<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tomcat持久化会话解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .highlight {
            position: relative;
        }
        .highlight::before {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(110, 142, 251, 0.2);
            z-index: -1;
        }
        .code-block {
            background-color: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #6e8efb;
        }
        .mermaid-container {
            background-color: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6">Tomcat持久化会话深度解析</h1>
                <p class="text-xl opacity-90 mb-8">构建高可用、可扩展的会话管理机制</p>
                <div class="flex justify-center space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-database mr-2"></i>数据持久化</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-server mr-2"></i>集群部署</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-shield-alt mr-2"></i>高可用性</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-5xl">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="h-1 w-12 bg-purple-500 rounded-full mr-4"></div>
                <h2 class="text-2xl font-bold">核心概念</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-8">
                <p class="text-lg mb-4"><span class="highlight font-medium">Tomcat 的持久化会话（Persistent Session）</span>指的是将会话数据保存在持久化存储中，以便在服务器重启或会话超时后能够恢复会话状态。</p>
                <p class="text-gray-700">这种机制对于需要保持用户状态和数据的一致性，特别是在负载均衡和集群环境下尤为重要。</p>
                
                <!-- Mermaid Diagram -->
                <div class="mermaid-container mt-8">
                    <div class="mermaid">
                        graph TD
                            A[客户端] --> B[Tomcat实例1]
                            A --> C[Tomcat实例2]
                            B --> D[数据库/文件存储]
                            C --> D
                            D --> E[持久化会话数据]
                    </div>
                </div>
            </div>
        </section>

        <!-- Implementation -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="h-1 w-12 bg-purple-500 rounded-full mr-4"></div>
                <h2 class="text-2xl font-bold">实现原理</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- JDBC Store -->
                <div class="card-hover bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center text-blue-600 mr-4">
                                <i class="fas fa-database text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold">数据库持久化会话</h3>
                        </div>
                        <p class="text-gray-700 mb-4">Tomcat 支持通过 <code>Store</code> 接口将会话数据持久化到关系型数据库中。</p>
                        
                        <div class="bg-gray-100 p-4 rounded-lg mb-4 code-block">
                            <pre><code class="text-sm">&lt;Context&gt;
    &lt;Manager className="org.apache.catalina.session.PersistentManager"
             maxIdleSwap="30"
             maxIdleBackup="30"&gt;
        &lt;Store className="org.apache.catalina.session.JDBCStore"
               driverClassName="com.mysql.cj.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/tomcat_sessions"
               username="root"
               password="password"
               sessionTable="sessions"
               sessionIdColumn="session_id"
               sessionDataColumn="session_data"
               sessionValidColumn="session_valid"/&gt;
    &lt;/Manager&gt;
&lt;/Context&gt;</code></pre>
                        </div>
                        
                        <div class="flex items-start text-gray-600">
                            <i class="fas fa-info-circle text-blue-500 mt-1 mr-2"></i>
                            <p>配置在 <code>conf/context.xml</code> 或 <code>META-INF/context.xml</code> 文件中</p>
                        </div>
                    </div>
                    <div class="bg-gray-50 px-6 py-4 border-t border-gray-200">
                        <h4 class="font-bold mb-2 text-gray-800">工作原理：</h4>
                        <ol class="list-decimal list-inside space-y-1 text-gray-700">
                            <li>会话数据在创建时存储到指定的数据库表中</li>
                            <li>每次会话更新时同步到数据库</li>
                            <li>服务器重启或会话超时后从数据库恢复数据</li>
                        </ol>
                    </div>
                </div>
                
                <!-- File Store -->
                <div class="card-hover bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center text-purple-600 mr-4">
                                <i class="fas fa-file-alt text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold">文件持久化会话</h3>
                        </div>
                        <p class="text-gray-700 mb-4">Tomcat 也可以将会话数据持久化到文件系统中。</p>
                        
                        <div class="bg-gray-100 p-4 rounded-lg mb-4 code-block">
                            <pre><code class="text-sm">&lt;Context&gt;
    &lt;Manager className="org.apache.catalina.session.PersistentManager"
             maxIdleSwap="30"
             maxIdleBackup="30"&gt;
        &lt;Store className="org.apache.catalina.session.FileStore"
               directory="session-store"/&gt;
    &lt;/Manager&gt;
&lt;/Context&gt;</code></pre>
                        </div>
                        
                        <div class="flex items-start text-gray-600">
                            <i class="fas fa-info-circle text-purple-500 mt-1 mr-2"></i>
                            <p>配置在 <code>conf/context.xml</code> 或 <code>META-INF/context.xml</code> 文件中</p>
                        </div>
                    </div>
                    <div class="bg-gray-50 px-6 py-4 border-t border-gray-200">
                        <h4 class="font-bold mb-2 text-gray-800">工作原理：</h4>
                        <ol class="list-decimal list-inside space-y-1 text-gray-700">
                            <li>会话数据在创建时存储到指定目录下的文件中</li>
                            <li>每次会话更新时同步到文件系统</li>
                            <li>服务器重启或会话超时后从文件系统恢复数据</li>
                        </ol>
                    </div>
                </div>
            </div>
        </section>

        <!-- Application Scenarios -->
        <section>
            <div class="flex items-center mb-6">
                <div class="h-1 w-12 bg-purple-500 rounded-full mr-4"></div>
                <h2 class="text-2xl font-bold">应用场景</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <!-- Cluster Environment -->
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center text-blue-600 mb-4">
                        <i class="fas fa-network-wired text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">集群环境</h3>
                    <p class="text-gray-700">在负载均衡的集群环境中，各个 Tomcat 实例通常需要共享会话数据。持久化会话确保即使用户请求被转发到不同的 Tomcat 实例上，也能够恢复之前的会话状态。</p>
                </div>
                
                <!-- High Availability -->
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <div class="w-12 h-12 rounded-full bg-green-100 flex items-center justify-center text-green-600 mb-4">
                        <i class="fas fa-shield-alt text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">高可用性</h3>
                    <p class="text-gray-700">在需要高可用性的系统中，Tomcat 实例可能会因故障而重启或替换。持久化会话使得在服务器重启或替换后，用户的会话数据能够恢复。</p>
                </div>
                
                <!-- Long-running Sessions -->
                <div class="card-hover bg-white rounded-xl shadow-md p-6">
                    <div class="w-12 h-12 rounded-full bg-purple-100 flex items-center justify-center text-purple-600 mb-4">
                        <i class="fas fa-clock text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">长时间运行的会话</h3>
                    <p class="text-gray-700">对于需要长时间保持会话状态的应用，如在线购物车、长时间的用户登录等，持久化会话可以有效防止会话数据丢失。</p>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-400 py-8">
        <div class="container mx-auto px-6 text-center">
            <div class="mb-4">
                <h3 class="text-white text-lg font-medium mb-2">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors">http://www.yuque.com/jtostring</a>
            </div>
            <div class="flex justify-center space-x-6 text-xl">
                <a href="#" class="hover:text-white transition-colors"><i class="fab fa-github"></i></a>
                <a href="#" class="hover:text-white transition-colors"><i class="fab fa-twitter"></i></a>
                <a href="#" class="hover:text-white transition-colors"><i class="fab fa-linkedin"></i></a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 微交互效果
        document.querySelectorAll('.card-hover').forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.classList.add('shadow-lg');
            });
            card.addEventListener('mouseleave', function() {
                this.classList.remove('shadow-lg');
            });
        });
    </script>
</body>
</html>